<template>
  <div class="cart_container">
    <div class="mui-card" v-for="(item,i) in list" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner goodlist">
          <!-- 选中按钮 -->
          <mt-switch
            v-model="$store.getters.selected[item.id]"
            @change="selected(item.id,$store.getters.selected[item.id])"
          ></mt-switch>
          <img :src="item.thumb_path" alt width="60px" height="60px">
          <div class="info">
            <h1>{{item.title}}</h1>
            <p>
              <span class="price">{{item.sell_price}}</span>
              <numbox :initCount="$store.getters.CartCount[item.id]" :id="item.id"></numbox>
              <a href="javascript:;" @click.prevent="remove(item.id,i)">删除</a>
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.amountAndPrice.amount }}</span>件，总价
              <span class="red">￥{{ $store.getters.amountAndPrice.price }}</span>元
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import numbox from "../subcomponents/Cart-numbox.vue";
export default {
  created() {
    this.getCart();
  },
  data() {
    return {
      list: []
    };
  },
  methods: {
    getCart() {
      var id = [];
      this.$store.state.car.forEach(item => {
        id.push(item.id);
      });
      //   console.log(id)
      if(id.length<=0){
        return
      }
      this.$http.get("api/goods/getshopcarlist/" + id.join(",")).then(data => {
        if (data.body.status === 0) {
          console.log(data.body.message);
          this.list = data.body.message;
        }
      });
    },
    // 删除按钮功能
    remove(id, i) {
      this.list.splice(i, 1);
      this.$store.commit("remove", id);
    },
    // 选中按钮功能
    selected(id, isSelected) {
    //   console.log(id, isSelected);
      var select = {
        id: id,
        selected: isSelected
      };
      this.$store.commit("updateSelected", select);
    }
  },
  components: {
    numbox
  }
};
</script>
<style lang="scss" scoped>
.cart_container {
  background-color: #eee;
  overflow: hidden;
  .goodlist {
    display: flex;
    align-content: center;
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      h1 {
        font-size: 13px;
        font-weight: bold;
      }
      p {
        .price {
          color: red;
          font-weight: bold;
        }
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    .left {
      .red {
        color: red;
      }
    }
  }
}
</style>
